export default {
	state: {
		list: JSON.parse(localStorage.getItem('plist-2')) || [],
		/* 	[
				{
					id: 100,
					name: "吃饭",
					isDone: true,
				},
				{
					id: 201,
					name: "睡觉",
					isDone: false,
				},
				{
					id: 103,
					name: "打豆豆",
					isDone: true,
				},
			], */
		type: 'all',
	},
	mutations: {
		//修改那个状态
		changeDone(state, id) {
			const obj = state.list.find(item => item.id === id)
			obj.isDone = !obj.isDone
		},
		//删除
		del(state, id) {
			state.list = state.list.filter(item => item.id !== id)
		},
		//增加
		add(state, content) {
			state.list.unshift({
				id: +new Date(),
				name: content,
				isDone: false
			})
		},
		//全选
		change(state, val) {
			state.list.forEach((item) => (item.isDone = val));
		},
		//清除
		clear(state) {
			state.list = state.list.filter(item => !item.isDone)
		},
		//修改type
		changeType(state, val) {
			state.type = val
		}
	},
	actions: {
	},
	getters: {
		leftCount(state) {
			return state.list.filter(item => !item.isDone).length
		},
		isShow(state) {
			return state.list.some(item => item.isDone)
		},
		newList(state) {
			if (state.type === "all") {
				return state.list;
			} else if (state.type === "ndone") {
				return state.list.filter((item) => !item.isDone);
			} else if (state.type === "done") {
				return state.list.filter((item) => item.isDone);
			}
		},
	},
	namespaced: true
}